﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KetBanBonPhuong;

public partial class UserControls_Album : System.Web.UI.UserControl
{
    private static String StrAlbumPath = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["AlbumID"] != null)
        {
            if (!IsPostBack)
            {
                int AlbumID = Convert.ToInt32(Request.QueryString["AlbumID"].ToString());
                Album album = Album.GetInfo(AlbumID);
                StrAlbumPath = album.Path;
                albumNameLabel.Text = album.AlbumName;
                User user;
                // kiem tra co user dang dang nhap ko
                if (Session["UserID"] != null)
                {
                    user = User.GetInfo(Convert.ToInt32(Session["UserID"].ToString()));
                    // neu co kiem tra xem co phai album cua user ko
                    DataTable dt = Album.GetByUserCreated(user.UserID);
                    foreach (DataRow row in dt.Rows)
                    {
                        if (Convert.ToInt32(row["album_id"].ToString()) == AlbumID)
                        {
                            return;
                        }
                    }
                }
                else // neu ko co user dang nhap dua ve trang default
                {
                    Response.Redirect("Default.aspx");
                }
                
                // neu dang xem bo anh cua ng kahc
                if (Request.QueryString["UserID"] == null)
                {
                    // kiem tra xem bo anh cua dung cua nguoi do hay ko
                    DataTable dt = Album.GetByUserCreated(Convert.ToInt32(Request.QueryString["UserID"].ToString()));
                    foreach (DataRow row in dt.Rows)
                    {
                        if (Convert.ToInt32(row["album_id"].ToString()) == AlbumID)
                        {
                            return;
                        }
                    }
                    Response.Redirect("Errors.aspx");
                }
                else // 
                {
                    // dua ve trang loi
                    Response.Redirect("Errors.aspx");
                }
            }
        }
    }
    protected void Repeater1_PreRender(object sender, EventArgs e)
    {
        if (Request.QueryString["AlbumID"] != null)
        {
            if (!IsPostBack)
            {
                int AlbumID = Convert.ToInt32(Request.QueryString["AlbumID"].ToString());
                Repeater1.DataSource = Photo.GetByAlbum(AlbumID);
                Repeater1.DataBind();
            }
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0)
        {
            return;
        }

        DataRowView row = (DataRowView)e.Item.DataItem;
        HyperLink photoDetailHyperLink = (HyperLink)e.Item.FindControl("photoDetailHyperLink");
        photoDetailHyperLink.NavigateUrl = "../PhotoDetail.aspx?PhotoID="+row["photo_id"].ToString(); // redirect sang trang photo detail
        Image photoImage = (Image)e.Item.FindControl("photoImage");
        photoImage.ImageUrl = "../Files/Album/" + StrAlbumPath + row["name"].ToString();
        if (e.Item.ItemIndex % 4 == 3)
        {
            Literal brLiteral = (Literal)e.Item.FindControl("brLiteral");
            brLiteral.Text = "<br />";
        }
    }
}
